//==============================================================================
//
//               Copyright (C) 2006  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2006-09-11 18:08:00 $ $Revision: 1.6 $
//
//==============================================================================

#include "ofc/DFTPClient.h"

#include "DTest.h"

#include "ofc/DFile.h"

//-Network---------------------------------------------------------------------

 
void DFTPClient_test()
{
  DFTPClient         *client    = [DFTPClient new];
  DInetSocketAddress *address   = [DInetSocketAddress new];
  DText              *directory = [DText new];
  DList              *list      = [DList new];
  DFile              *file      = [DFile new];
  
  STARTTEST();

  [address host :"localhost" :DFPC_PORT];

  // [client active :20];
  
  TEST(![client isConnected]);
  
  TEST([client open :address]);
       
  TEST([client isConnected]);

  if ([client isConnected])
  {
    
    TEST([client login :NULL :NULL :NULL]);

    [client help :NULL];
    
    [client state];

    TEST([client systemInfo]);
    
    [client getWorkingDirectory];

    TEST(strcmp([client directory], "/") == 0);

    TEST([client changeCurrentDirectory :"pub"]);
    
    if ([client makeDirectory :"temp"])
    {
      TEST(strcmp([client directory], "/pub/temp") == 0);
      
      [directory set :[client directory]];
      
      [client getWorkingDirectory];
      
      TEST([client changeCurrentDirectory :[directory cstring]]);
      
      TEST([client changeCurrentToParent]);
      
      TEST(strcmp([client directory], "/pub") == 0);

      TEST([client list :NULL :nil]);
      
      TEST([client nameList :NULL :list]); 
      
      TEST([list length] > 0);

      if ([file open :"test.m" :"r"])
      {
        TEST([client overwriteASCII :"test.m" :file]);
        
        [file seek :0 :0];
        
        TEST([client appendASCII :"test.m" :file]);
        
        [file close];
      }
      
      TEST([client removeDirectory :"temp"]);
      
      if ([file open :"test.m2" :"w"])
      {
        TEST([client retrieveASCII :"DDirectory.m" :file]);
        
        printf("Bytes: %d\n", [client bytes]);
        
        [file close];
      }
    }
    else
    {
      TEST(NO);
    }
    
    [client quit];
  }

  [list    free];
  [client  free];
  [address free];
  
  STOPTEST();
}
